package com.yinhai.adapter;

import java.util.List;

import android.content.AsyncQueryHandler;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yinhai.activity.R;
import com.yinhai.activity.SMSDetailActivity;
import com.yinhai.model.PhoneRecord;

public class DialAllRecordAdapter extends BaseAdapter {

	private Context ctx;
	private List<PhoneRecord> list;
	private LayoutInflater inflater;

	public DialAllRecordAdapter(Context context, List<PhoneRecord> list) {
		this.ctx = context;
		this.list = list;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.dial_poprec_list_item, null);
			holder = new ViewHolder();
			holder.call_type = (ImageView) convertView
					.findViewById(R.id.dial_head_arrow);
			holder.name = (TextView) convertView
					.findViewById(R.id.dial_item_name);
			holder.number = (TextView) convertView
					.findViewById(R.id.dial_item_tel);
			holder.date = (TextView) convertView
					.findViewById(R.id.dial_item_date);
			holder.time = (TextView) convertView
					.findViewById(R.id.dial_item_time);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		PhoneRecord phoneRecord = list.get(position);
		String phoneType = phoneRecord.getType();
		if ("1".equals(phoneType)) {
			holder.call_type.setImageResource(R.drawable.dial_incomming_normal);
			if(!phoneRecord.getDuration().equals("0")){
				holder.time.setText(getDataStr(phoneRecord.getDuration()));
			}else{
				holder.time.setText("未接通");
			}
		} else if ("2".equals(phoneType)) {
			holder.call_type.setImageResource(R.drawable.dial_outgoing_nomal);
			if(!phoneRecord.getDuration().equals("0")){
				holder.time.setText(getDataStr(phoneRecord.getDuration()));
			}else{
				holder.time.setText("未接通");
			}
		} else if ("3".equals(phoneType)) {
			holder.call_type.setImageResource(R.drawable.dial_missed_normal);
			holder.time.setText("响铃0秒");
			
		} else if ("4".equals(phoneType)) {
			holder.call_type.setImageResource(R.drawable.dial_reject);
			holder.time.setText("响铃0秒");
		}
		holder.name.setText(phoneRecord.getName());
		holder.number.setText(phoneRecord.getTelPhone());
		// 设置号码
		holder.number.setTag(phoneRecord.getTelPhone());
		//holder.time.setText(phoneRecord.getDuration());
		holder.date.setText(phoneRecord.getYearMonth()+" "+phoneRecord.getTime());

		//addViewListener(holder.msg_btn, phoneRecord, position);

		return convertView;
	}

	public static class ViewHolder {
		ImageView call_type;
		TextView name;
		TextView number;
		TextView date;
		TextView time;
		ImageView msg_btn;

		public String getTelPhone() {
			return (String) number.getTag();
		}
		public String getName(){
			return name.getText().toString();
		}
	}

	// 通过电话号码异步查询thread_id
	private static class MyAsynQueryHandler extends AsyncQueryHandler {
		private Context contextt;

		public MyAsynQueryHandler(Context context) {
			super(context.getContentResolver());
			contextt = context;
		}

		@Override
		protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
			super.onQueryComplete(token, cookie, cursor);
			if (cursor.getCount() > 0) {
				cursor.moveToFirst();
				long threadId = cursor.getLong(cursor
						.getColumnIndex("thread_id"));
				String address = cursor.getString(cursor
						.getColumnIndex("address"));
				cursor.close();
				Intent intent = new Intent(contextt, SMSDetailActivity.class);
				intent.putExtra("threadId", String.valueOf(threadId));
				intent.putExtra("address", address);
				contextt.startActivity(intent);
			}
		}

	};

	private void addViewListener(View view, final PhoneRecord phoneRecord,
			final int position) {
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String telNo = phoneRecord.getTelPhone();
				Uri uri = Uri.parse("content://sms");
				String[] projection = new String[] { "thread_id", "address" }; // 查询的列
				new MyAsynQueryHandler(ctx).startQuery(0, null, uri,
						projection, "address = " + telNo, null, null);

				// Uri uri = Uri.parse("smsto:" + );
				// Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
				// ctx.startActivity(intent);
			}
		});
	}
	
	String getDataStr(String data){
		String timeStr = "";
		int time = Integer.parseInt(data);  
        int hour = 0;  
        int minute = 0;  
        int second = 0;  
        if (time <= 0)  
            return "00:00";  
        else {  
            minute = time / 60;  
            if (minute < 60) {  
                second = time % 60;  
                if(minute==0){
                	timeStr = "通话"+ second+"秒";  
                }else{
                	timeStr = "通话"+minute + "分" + second+"秒";  
                }
                
            } else {  
                hour = minute / 60;  
                if (hour > 99)  
                    return "99:59:59";  
                minute = minute % 60;  
                second = time - hour * 3600 - minute * 60;  
                timeStr = "通话"+hour + "小时" + minute + "分" + second+"秒";  
            }  
        }  
        return timeStr;
	}
	public static String unitFormat(int i) {  
        String retStr = null;  
        if (i >= 0 && i < 10)  
            retStr = "0" + Integer.toString(i);  
        else  
            retStr = "" + i;  
        return retStr;  
    } 
}
